package com.example.forumsystem.demos.web.controller;

import com.example.forumsystem.demos.web.common.ResultCode;
import com.example.forumsystem.demos.web.exception.ApplicationException;
import com.example.forumsystem.demos.web.model.BoardInfo;
import com.example.forumsystem.demos.web.model.Result;
import com.example.forumsystem.demos.web.service.IBoardService;
import com.example.forumsystem.demos.web.service.IUserService;
import io.micrometer.core.lang.NonNull;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Api("板块相关的接口")
@RequestMapping("/board")
@RestController
@Slf4j
public class BoardController {
    @Autowired
    private IBoardService boardService;
    @Value("${forumSystem.param.limitBoardNum:9}")
    private Integer limitBoardnum;

    @ApiOperation("读取首页板块列表")
    @RequestMapping("/selectBoardByNum")
    public List<BoardInfo> selectBoardByNum(){
        List<BoardInfo> boardInfos = boardService.selectBoardByNum(limitBoardnum);
        if (boardInfos == null){
            log.info("限制后，导航栏内没有数据;限制的个数为：" + limitBoardnum);
            boardInfos = new ArrayList<>();
        }
        return boardInfos;
    }

    @ApiOperation("获取版块信息")
    @RequestMapping("/getById")
    public BoardInfo getById (@ApiParam("版块Id") @RequestParam("id") @NonNull Long id) {
        BoardInfo board = boardService.selectById(id);
        if (board == null || board.getDeleteState() == 1) {
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            throw new ApplicationException(Result.fail(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }
        return board;
    }
}
